package com.practice.mybatis.v2.mapper;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import com.practice.mybatis.v2.config.MapperRegistory;
import com.practice.mybatis.v2.session.SqlSession;

public class MapperProxy<T> implements InvocationHandler {
	
	private final SqlSession sqlSession;
	
	private final Class<T> clazz;

	public MapperProxy(SqlSession sqlSession, Class<T> clazz) {
		this.sqlSession = sqlSession;
		this.clazz = clazz;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		MapperRegistory.MapperData<T> mapperData = sqlSession.getConfiguration().getRegistory()
				.get(method.getDeclaringClass().getName() + "." + method.getName());
		if (mapperData != null) {
			System.out.println(String.format("SQL [ %s ], parameter [ %s ]", mapperData.getSql(), args[0]));
			return sqlSession.selectOne(mapperData, String.valueOf(args[0]));
		}
		return method.invoke(proxy, args);
	}
	

}
